import 'dart:io';
import 'dart:async';

import 'package:core_package/core_package.dart';

class SocketManage {
  static String host = '192.168.4.1';
  static int port = 9080;
  static Socket? mSocket;
  static Stream<List<int>>? mStream;

  static Future<bool?> initSocket() async {
    await Socket.connect(host, port, timeout: Duration(minutes: 5)).then((Socket socket) {
      print("建立成功");
      mSocket = socket;
      return true;
      // mStream = mSocket.asBroadcastStream(); //多次订阅的流 如果直接用socket.listen只能订阅一次
    }).catchError((e) {
      print('connectException:$e');
      ShowToastWidget.showToast(msg: "链接失败，请到把手机链接wifi \'niuniulife\'");
      // initSocket();
      return false;
    });
  }

  static void addParams(List<int> params) {
    mSocket?.add(params);
  }

  static void dispos() {
    mSocket?.close();
    mSocket = null;
  }
}
